import requests
import urllib.parse
import re


class X23usCom:

    def __init__ (self,key_word='大主宰'):#默认关键字

        self.key_word = key_word

    
    def index(self):
        key_word = self.key_word.encode('gb2312')
        url = 'https://www.x23us.com/modules/article/search.php'
        params = {
            'searchtype':'keywords',
            'searchkey':key_word,
            }
        
        res = requests.get(url,params=params)
        print(res)
        
        if res.history:
            return {'url':res.url}
        else:
            return res

    def info(self):
        '''return:{'大主宰'：{author:土豆，url:},...}'''

        html = self.index().text

        book_name = '<td class="odd"><a href=".*?">(.*?)</a></td>'
        bookimg_url = '<td class="odd"><a href="(.*?)">.*?</a></td>'
        book_url ='<td class="even"><a href="https://www.x23us.com/html/(.*?)" target="_blank">.*?</a></td>'
        book_author = '<td class="odd">\w*?([^a]*?)</td>'
        book_statu = '<td class="even" align="center">(.*?)</td>'

        book_name = re.findall(book_name,html)

        book_url = re.findall(book_url,html)        

        book_author = re.findall(book_author,html)

        book_statu = re.findall(book_statu,html)

        info_dict = {}
        for i in range(len(book_statu)):
            info_dict[book_name[i].replace('<b style="color:red">','').\
                      replace('</b>','')] = \
                      {'author':book_author[i].replace('<b style="color:red">','').\
                      replace('</b>',''),
                       'url':book_url[i],
                       'statu':book_statu[i]
                       }


        return info_dict

        

        

        

        
if __name__ =='__main__':
    pass

    #x = X23usCom(key_word='主宰').info()
























    
